package com.gopay.bis.service.bank.impl.ebank;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.BisPayCheckUtils;
import com.gopay.bis.common.util.constants.BisPayConstants;
import com.gopay.bis.manager.bank.ebank.BocomB2bManager;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BocomConstants;
import com.gopay.common.constants.bank.BocomOrderStatus;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.bocom.ebank.pay.BocomB2bMessage;
import com.gopay.common.domain.bank.bocom.ebank.pay.BocomB2bQueryDetail;
import com.gopay.common.domain.bank.bocom.ebank.pay.b2b.req.BocomB2bReq;
import com.gopay.common.domain.bank.bocom.ebank.pay.b2b.res.BocomB2bRes;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankInteractRemoteService;

public class BocomB2bBankServiceImpl implements BankInteractRemoteService {
	
	@Resource(name = "bocomB2bManager")
	private BocomB2bManager bocomB2bManager;

	@Override
	public Result pay(BaseMessage base) {
		Result result = new Result(false);
		try {
			BocomB2bReq messageB2B = new BocomB2bReq();
			BeanUtils.copyProperties(base, messageB2B);
			messageB2B = (BocomB2bReq) bocomB2bManager.sign(messageB2B);
			base.setBankOrderId(base.getOrderId());
			result.setDefaultModel(messageB2B);
			result.setSuccess(true);
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6028);
		}
		return result;
	}

	@Override
	public Result bankBack(BaseMessage base) {
		Result result = new Result(false);
		try {
			// B2B验签
			BocomB2bReq messageB2B = new BocomB2bReq();
			BeanUtils.copyProperties(base, messageB2B);
			Boolean flag = (Boolean) bocomB2bManager.verify(messageB2B);
			result.setDefaultModel(flag);			
			result.setSuccess(true);
			result.setModel("res", messageB2B);

			Map<String, String> param = new HashMap<String, String>();
			// 验签成功入账
			if (messageB2B.getRetCode().equals("1")) {				
				param.put("orderId", messageB2B.getOrderId());
				param.put("amt", messageB2B.getAmount().toString());
				param.put("cardType", "Unknown");
				param.put("notifyNum", "1");
				param.put("payStatus", "00");
				param.put("retCode", "00");
				param.put("retMsg", "验签成功");
				param.put("signData", messageB2B.getSignMessage());
				param.put("isFront", String.valueOf(base.isFront()));
				result.setModel("isNotifyCashier", param);
				result.setOrderStatus(true);
			} else {
				param.put("orderId", messageB2B.getOrderId());
				param.put("amt", messageB2B.getAmount().toString());
				param.put("cardType", "Unknown");
				param.put("notifyNum", "1");
				param.put("payStatus", "01");
				param.put("retCode", "01");
				param.put("retMsg", "验签失败");
				param.put("signData", messageB2B.getSignMessage());
				param.put("isFront", String.valueOf(base.isFront()));
				result.setModel("isNotifyCashier", param);
				result.setOrderStatus(false);
			}
		
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6064);
		}
		return result;
	}

	@Override
	public Result query(BaseMessage base) {
		// B2B查询
		Result result = new Result(false);
		try {
			BocomB2bReq messageB2B = new BocomB2bReq();
			BeanUtils.copyProperties(base, messageB2B);
			BocomB2bReq res = bocomB2bManager.query(messageB2B);
			result.setDefaultModel(res);
			result.setSuccess(true);
			if(BisPayCheckUtils.checkExternalOrderID(messageB2B.getOrderId())){
				Map<String, String> param = new HashMap<String, String>();
				param.put("orderId", messageB2B.getOrderId());
				param.put("amount", messageB2B.getAmount().toString());
				if (res == null || res.getRetCode() == null) {
					// 查询订单为空
					param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
					param.put("returnMessage", "银行返回对账结果为空");
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					result.setModel("queryResult", param);
					return result;
				}
				// 通信状态码错误
				if (!res.getRetCode().equals(BocomConstants.B2C_RETCODE_SUCCESS)) {
					param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
					param.put("returnMessage", res.getErrorMessage());
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					result.setModel("queryResult", param);
					return result;
				}
				if (res.getQueryDetails() != null && res.getQueryDetails().size() != 0) {
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);					
					BocomB2bQueryDetail orderRes = res.getQueryDetails().get(0);
//					// 如果没有订单状态，输出错误信息
//					if ("".equals(orderRes.getOrderState()) || orderRes.getOrderState() == null) {
//						param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
//						param.put("returnMessage", res.getErrorMessage());
//					}else if (orderRes.getOrderState().equals(BocomConstants.B2B_ORDERSTATE_ORDERCLOSE)) {
//						//成功处理
//						param.put("tradeState", BisPayConstants.TRADESTATE_SUCCESS);
//						param.put("returnMessage", "交易成功");
//					}else {
//						//不处理
//						param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
//						param.put("returnMessage", BocomOrderStatus.getName(orderRes.getOrderState()));
//					}
					//交易成功
					if(orderRes.getOrderState().equals(BocomConstants.TRANSTATE_SUCCESS)){
						param.put("tradeState", BisPayConstants.TRADESTATE_SUCCESS);
						param.put("returnMessage", "交易成功");
					}
					//处理中
					else if(orderRes.getOrderState().equals(BocomConstants.TRANSTATE_PROCESSING)){
						param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
						param.put("returnMessage", "交易处理中");
					}
					//失败处理
					else if(orderRes.getOrderState().equals(BocomConstants.TRANSTATE_FAIL)){
						param.put("tradeState", BisPayConstants.TRADESTATE_FAIL);
						param.put("returnMessage", res.getErrorMessage());
					}
					//不处理
					else{
						param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
						param.put("returnMessage", BocomOrderStatus.getName(orderRes.getOrderState()));
					}
				}else{
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);			
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					param.put("returnMessage", "未查找到订单信息，订单查询返回码："+ res.getRetCode());
				}
				result.setModel("queryResult", param);
			}
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6032);
		}
		return result;
	}

	@Override
	public Result refund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result dayend(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

	public BocomB2bManager getBocomB2bManager() {
		return bocomB2bManager;
	}

	public void setBocomB2bManager(BocomB2bManager bocomB2bManager) {
		this.bocomB2bManager = bocomB2bManager;
	}

	@Override
	public Result queryBack(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

    @Override
    public Result refundBack(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result refundQuery(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

	@Override
	public Result sendBankRefund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

}
